/*
 * -----------------------------------------------------------------
 * Programmer(s): Daniel R. Reynolds and Ting Yan @ SMU
 *      Based on idaFoodWeb_bnd.c and parallelized with OpenMP
 * -----------------------------------------------------------------
 * SUNDIALS Copyright Start
 * Copyright (c) 2002-2023, Lawrence Livermore National Security
 * and Southern Methodist University.
 * All rights reserved.
 *
 * See the top-level LICENSE and NOTICE files for details.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 * SUNDIALS Copyright End
 * -----------------------------------------------------------------
 * IDA例子程序 : Food web problem.
 *
 * 此示例程序(OpenMP 版本) 使用 SUNBAND linear solver,
 * 以及 IDACalcIC 计算初始条件.
 *
 * 此示例求解的数学问题是 DAE system(微分代数方程组)
 * 将 偏微分方程系统 作空间离散化得到
 * PDE 系统是 food web population model,
 * with predator-prey(捕食者-猎物) 相互作用 and 扩散, 在二维单位方块上.
 * The dependent variable vector is:
 *
 *         1   2         ns
 *   c = (c , c ,  ..., c  ) , ns = 2 * np
 *
 * and the PDE's are as follows:
 *
 *     i             i      i
 *   dc /dt = d(i)*(c    + c  )  +  R (x,y,c)   (i = 1,...,np)
 *                   xx     yy       i
 *
 *              i      i
 *   0 = d(i)*(c    + c  )  +  R (x,y,c)   (i = np+1,...,ns)
 *              xx     yy       i
 *
 *   where the reaction terms R are:
 *
 *                   i             ns         j
 *   R  (x,y,c)  =  c  * (b(i)  + sum a(i,j)*c )
 *    i                           j=1
 *
 * The number of species is ns = 2 * np, with the first np being
 * prey and the last np being predators. The coefficients a(i,j),
 * b(i), d(i) are:
 *
 *  a(i,i) = -AA   (all i)
 *  a(i,j) = -GG   (i <= np , j >  np)
 *  a(i,j) =  EE   (i >  np, j <= np)
 *  all other a(i,j) = 0
 *  b(i) = BB*(1+ alpha * x*y + beta*sin(4 pi x)*sin(4 pi y)) (i <= np)
 *  b(i) =-BB*(1+ alpha * x*y + beta*sin(4 pi x)*sin(4 pi y)) (i  > np)
 *  d(i) = DPREY   (i <= np)
 *  d(i) = DPRED   (i > np)
 *
 * The various scalar parameters required are set using '#define'
 * statements or directly in routine InitUserData. In this program,
 * np = 1, ns = 2. The boundary conditions are homogeneous Neumann:
 * normal derivative = 0.
 *
 * A polynomial in x and y is used to set the initial values of the
 * first np variables (the prey variables) at each x,y location,
 * while initial values for the remaining (predator) variables are
 * set to a flat value, which is corrected by IDACalcIC.
 *
 * The PDEs are discretized by central differencing on a MX by MY
 * mesh.
 *
 * The DAE system is solved by IDA using the SUNBAND linear solver.
 * Output is printed at t = 0, .001, .01, .1, .4, .7, 1.
 *
 * Optionally, we can set the number of threads from environment
 * variable or command line. To check the current value for number
 * of threads from environment:
 *      % echo $OMP_NUM_THREADS
 *
 * Execution:
 *
 * To use the default value for the number of threads from
 * the OMP_NUM_THREADS environment value:
 *      % ./idaFoodWeb_bnd_omp
 * To specify the number of threads at the command line, use
 *      % ./idaFoodWeb_bnd_omp num_threads
 * where num_threads is the desired number of threads.
 *
 * -----------------------------------------------------------------
 * References:
 * [1] Peter N. Brown and Alan C. Hindmarsh,
 *     Reduced Storage Matrix Methods in Stiff ODE systems, Journal
 *     of Applied Mathematics and Computation, Vol. 31 (May 1989),
 *     pp. 40-91.
 *
 * [2] Peter N. Brown, Alan C. Hindmarsh, and Linda R. Petzold,
 *     Using Krylov Methods in the Solution of Large-Scale
 *     Differential-Algebraic Systems, SIAM J. Sci. Comput., 15
 *     (1994), pp. 1467-1488.
 *
 * [3] Peter N. Brown, Alan C. Hindmarsh, and Linda R. Petzold,
 *     Consistent Initial Condition Calculation for Differential-
 *     Algebraic Systems, SIAM J. Sci. Comput., 19 (1998),
 *     pp. 1495-1512.
 * -----------------------------------------------------------------
 */

#include <ida/ida.h>
#include <math.h>
#include <nvector/nvector_openmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <sundials/sundials_direct.h>
#include <sundials/sundials_types.h>
#include <sunlinsol/sunlinsol_band.h>
#include <sunmatrix/sunmatrix_band.h>
#include <sunmatrix/sunmatrix_dense.h>

#ifdef _OPENMP
#include <omp.h>
#endif

/* Problem Constants. */

#define NPREY 1               /* No. of prey (= no. of predators). */
#define NUM_SPECIES 2 * NPREY /*生物种类 总数*/

#define PI RCONST(3.1415926535898)
#define FOURPI (RCONST(4.0) * PI)

#define MX 20                       /* MX = number of x mesh points      */
#define MY 20                       /* MY = number of y mesh points      */
#define NSMX (NUM_SPECIES * MX)     /* X方向上的 自由度数*/
#define NEQ (NUM_SPECIES * MX * MY) /* 方程总数*/
#define AA RCONST(1.0)              /* Coefficient in above eqns. for a; a(i,i) */
#define EE \
    RCONST(10000.) /* Coefficient in above eqns. for a; a(i,j) =  EE (i >  np, j \
                    <= np)  */
#define GG \
    RCONST(0.5e-6) /* Coefficient in above eqns. for a; a(i,j) = -GG   (i <= np  \
                    , j >  np) */
/* Coefficient in above eqns. for b; b(i) = BB*(1+ alpha * x*y + beta*sin(4 pi
 * x)*sin(4 pi y)) (i <= np)*/
#define BB RCONST(1.0)
#define DPREY RCONST(1.0)  /* Coefficient in above eqns. for d; d(i) = DPREY   (i <= np) */
#define DPRED RCONST(0.05) /* Coefficient in above eqns. for d; d(i) = DPRED   (i > np)*/
#define ALPHA RCONST(50.)  /* Coefficient alpha in above eqns.  */
#define BETA RCONST(1000.) /* Coefficient beta in above eqns.   */
#define AX RCONST(1.0)     /* Total range of x variable         */
#define AY RCONST(1.0)     /* Total range of y variable         */
#define RTOL RCONST(1.e-5) /* Relative tolerance                */
#define ATOL RCONST(1.e-5) /* Absolute tolerance                */
#define NOUT 6             /* Number of output times            */
#define TMULT RCONST(10.0) /* Multiplier for tout values        */
#define TADD RCONST(0.3)   /* Increment for tout values         */
#define ZERO RCONST(0.)    /*constant*/
#define ONE RCONST(1.0)    /*constant*/

/*
 * User-defined vector and accessor macro: IJ_Vptr.
 * IJ_Vptr is defined in order to express the underlying 3-D structure of
 * the dependent variable vector from its underlying 1-D storage (an N_Vector).
 * IJ_Vptr(vv,i,j) returns a pointer to the location in vv corresponding to
 * species index is = 0, x-index ix = i, and y-index jy = j.
 * Mat3(is,i,j), 使用 fortran index; species is, i行j列 对应的
 */
#define IJ_Vptr(vv, i, j) (&NV_Ith_OMP(vv, (i) * NUM_SPECIES + (j) * NSMX))

/* Type: UserData.  包含问题中的常数, etc. */
typedef struct
{
    sunindextype Neq, ns, np, mx, my;
    realtype dx, dy, **acoef;  // x,y 格子长度, 相互作用系数
    realtype cox[NUM_SPECIES], coy[NUM_SPECIES],
        bcoef[NUM_SPECIES];  // 扩散系数 dx, dy, bi
    N_Vector rates;          // 反应速率 Ri(x,y,c)
    int nthreads;            // 线程数
} *UserData;

/* Prototypes for functions called by the IDA Solver. */
static int resweb(realtype time, N_Vector cc, N_Vector cp, N_Vector resval, void *user_data);

/* Prototypes for private Helper Functions. */
static void InitUserData(UserData webdata);
static void SetInitialProfiles(N_Vector cc, N_Vector cp, N_Vector id, UserData webdata);
static void PrintHeader(sunindextype mu, sunindextype ml, realtype rtol, realtype atol);
static void PrintOutput(void *ida_mem, N_Vector c, realtype t);
static void PrintFinalStats(void *ida_mem);
static void Fweb(realtype tcalc, N_Vector cc, N_Vector crate, UserData webdata);
static void WebRates(realtype xx, realtype yy, realtype *cxy, realtype *ratesxy, UserData webdata);
static realtype dotprod(sunindextype size, realtype *x1, realtype *x2);
static int check_retval(void *returnvalue, char *funcname, int opt);

/*
 *--------------------------------------------------------------------
 * MAIN PROGRAM
 *--------------------------------------------------------------------
 */

int main(int argc, char *argv[])
{
    void *ida_mem;
    SUNMatrix A;
    SUNLinearSolver LS;
    UserData webdata;
    N_Vector cc, cp, id;
    int iout;
    int retval;  // 各种调用的 return value
    sunindextype mu, ml;
    realtype rtol, atol, t0, tout, tret;
    int num_threads;
    SUNContext ctx;

    ida_mem = NULL;
    A = NULL;
    LS = NULL;
    webdata = NULL;
    cc = cp = id = NULL;

    /* 设置使用的 threads 数目*/
    num_threads = 1;  // 默认值
#ifdef _OPENMP
    num_threads = omp_get_max_threads(); /* 使用 OMP_NUM_THREADS 环境变量的值 */
#endif
    if (argc > 1) /* 使用命令行的值, 如果用户已提供*/
        num_threads = (int)strtol(argv[1], NULL, 0);

    /* 创建本次模拟需要的 SUNDIALS context object */
    retval = SUNContext_Create(NULL, &ctx);
    if (check_retval(&retval, "SUNContext_Create", 1)) return (1);

    /* 分配并初始化 user data block webdata. */
    webdata = (UserData)malloc(sizeof *webdata);
    webdata->rates = N_VNew_OpenMP(NEQ, num_threads, ctx);
    // webdata->acoef = SUNDlsMat_newDenseMat(NUM_SPECIES, NUM_SPECIES);
    // deprecated macro
    webdata->acoef = SM_COLS_D(SUNDenseMatrix(NUM_SPECIES, NUM_SPECIES, ctx));
    webdata->nthreads = num_threads;  // 设置 omp 线程数

    InitUserData(webdata);  // 初始化PDE中的各种系数

    // 分配 N-vectors, 初始化 cc(浓度场), cp(浓度场导数) and id(代数/微分分量
    // 标记).
    cc = N_VNew_OpenMP(NEQ, num_threads, ctx);
    if (check_retval((void *)cc, "N_VNew_OpenMP", 0)) return (1);
    cp = N_VNew_OpenMP(NEQ, num_threads, ctx);
    if (check_retval((void *)cp, "N_VNew_OpenMP", 0)) return (1);
    id = N_VNew_OpenMP(NEQ, num_threads, ctx);
    if (check_retval((void *)id, "N_VNew_OpenMP", 0)) return (1);

    SetInitialProfiles(cc, cp, id, webdata);

    /* 设置 IDAInit 的其他输入 */
    t0 = ZERO;
    rtol = RTOL;
    atol = ATOL;

    /* 调用 IDACreate 和 IDAInit 以初始化 IDA*/
    ida_mem = IDACreate(ctx);
    if (check_retval((void *)ida_mem, "IDACreate", 0)) return (1);

    retval = IDASetUserData(ida_mem, webdata);
    if (check_retval(&retval, "IDASetUserData", 1)) return (1);

    retval = IDASetId(ida_mem, id);
    if (check_retval(&retval, "IDASetId", 1)) return (1);

    retval = IDAInit(ida_mem, resweb, t0, cc, cp);
    if (check_retval(&retval, "IDAInit", 1)) return (1);

    retval = IDASStolerances(ida_mem, rtol, atol);
    if (check_retval(&retval, "IDASStolerances", 1)) return (1);

    /* 设置带状矩阵 和 线性求解器, and attach to IDA. */
    mu = ml = NSMX;
    A = SUNBandMatrix(NEQ, mu, ml, ctx);
    if (check_retval((void *)A, "SUNBandMatrix", 0)) return (1);
    LS = SUNLinSol_Band(cc, A, ctx);
    if (check_retval((void *)LS, "SUNLinSol_Band", 0)) return (1);
    retval = IDASetLinearSolver(ida_mem, LS, A);
    if (check_retval(&retval, "IDASetLinearSolver", 1)) return (1);

    /* 调用 IDACalcIC(使用默认选项), 纠正初始值*/
    tout = RCONST(0.001);
    retval = IDACalcIC(ida_mem, IDA_YA_YDP_INIT, tout);
    if (check_retval(&retval, "IDACalcIC", 1)) return (1);

    /* 打印输出文件头, 基本参数, 初始值*/
    PrintHeader(mu, ml, rtol, atol);
    PrintOutput(ida_mem, cc, ZERO);

    /* Loop over iout, call IDASolve (normal模式), 打印选中的输出*/
    for (iout = 1; iout <= NOUT; iout++)
    {
        retval = IDASolve(ida_mem, tout, &tret, cc, cp, IDA_NORMAL);
        if (check_retval(&retval, "IDASolve", 1)) return (retval);

        PrintOutput(ida_mem, cc, tret);

        if (iout < 3)
            tout *= TMULT;
        else
            tout += TADD;
    }

    /* 打印最终的统计, free内存. */
    PrintFinalStats(ida_mem);
    printf("num_threads = %i\n\n", num_threads);

    /* Free memory */
    IDAFree(&ida_mem);
    SUNLinSolFree(LS);
    SUNMatDestroy(A);

    N_VDestroy_OpenMP(cc);
    N_VDestroy_OpenMP(cp);
    N_VDestroy_OpenMP(id);

    SUNDlsMat_destroyMat(webdata->acoef);
    N_VDestroy_OpenMP(webdata->rates);
    free(webdata);

    SUNContext_Free(&ctx);

    return (0);
}

/* 定义宏, 方便后文的可读性 */
#define acoef (webdata->acoef)
#define bcoef (webdata->bcoef)
#define cox (webdata->cox)
#define coy (webdata->coy)

/*
 *--------------------------------------------------------------------
 * FUNCTIONS CALLED BY IDA
 *--------------------------------------------------------------------
 */

/*
 * resweb: predator-prey 系统的 残差(residual)函数
 * typedef int (*IDAResFn)(realtype tt, N_Vector yy, N_Vector yp, N_Vector rr, *
 * void *user_data) 调用 Fweb 计算方程的右端项, 然后视情况填充 残差向量, 在 prey
 * 种类时，使用 cp 值 res: residual, 残差向量 cc: c, 浓度场的值 cp: c prime,
 * 浓度场的时间导数
 */
static int resweb(realtype tt, N_Vector cc, N_Vector cp, N_Vector res, void *user_data)
{
    sunindextype jx, jy, is, yloc, loc, np;
    realtype *resv, *cpv;
    UserData webdata;
    jx = jy = is = 0;
    webdata = (UserData)user_data;

    cpv = NV_DATA_OMP(cp);
    resv = NV_DATA_OMP(res);
    np = webdata->np;

    /* Call Fweb to set res to vector of right-hand sides. */
    Fweb(tt, cc, res, webdata);

    /* Loop over all grid points, setting residual values appropriately
     for differential or algebraic components.                        */
#pragma omp parallel for default(shared) private(jy, yloc, jx, loc, is) schedule(static) num_threads(webdata->nthreads)
    for (jy = 0; jy < MY; jy++)
    {
        yloc = NSMX * jy;
        for (jx = 0; jx < MX; jx++)
        {
            loc = yloc + NUM_SPECIES * jx;
            for (is = 0; is < NUM_SPECIES; is++)
            {
                if (is < np)
                    resv[loc + is] = cpv[loc + is] - resv[loc + is]; /*微分分量*/
                else
                    resv[loc + is] = -resv[loc + is]; /*代数分量*/
            }
        }
    }

    return (0);
}

/*
 *--------------------------------------------------------------------
 * PRIVATE FUNCTIONS
 *--------------------------------------------------------------------
 */

/*
 * InitUserData: Load problem constants in webdata (of type UserData).
 */
static void InitUserData(UserData webdata)
{
    sunindextype i, j, np;
    realtype *a1, *a2, *a3, *a4, dx2, dy2;

    webdata->mx = MX;
    webdata->my = MY;
    webdata->ns = NUM_SPECIES;
    webdata->np = NPREY;
    webdata->dx = AX / (MX - 1);  // cell length
    webdata->dy = AY / (MY - 1);
    webdata->Neq = NEQ;  // 方程数目

    /* Set up the coefficients a and b, and others found in the equations. */
    np = webdata->np;
    dx2 = (webdata->dx) * (webdata->dx);
    dy2 = (webdata->dy) * (webdata->dy);

    for (i = 0; i < np; i++)
    {
        a1 = &(acoef[i][np]);       // 右上角
        a2 = &(acoef[i + np][0]);   // 左下角
        a3 = &(acoef[i][0]);        // 左上角
        a4 = &(acoef[i + np][np]);  // 右下角
        /*  Fill in the portion of acoef in the four quadrants, row by row. */
        for (j = 0; j < np; j++)
        {
            *a1++ = -GG;   // 右上角
            *a2++ = EE;    // 左下角
            *a3++ = ZERO;  // 左下角
            *a4++ = ZERO;  // 右下角
        }

        /* Reset the diagonal elements of acoef to -AA. */
        acoef[i][i] = -AA;            // 对角线 上半
        acoef[i + np][i + np] = -AA;  // 对角线 下半

        /* Set coefficients for b and diffusion terms. */
        bcoef[i] = BB;
        bcoef[i + np] = -BB;
        // diffusion 项, Laplace 项 c_xx 和 c_yy 前面的系数
        cox[i] = DPREY / dx2;       // 被捕系数/dx^2
        cox[i + np] = DPRED / dx2;  // 捕食系数/dx^2
        coy[i] = DPREY / dy2;
        coy[i + np] = DPRED / dy2;
    }
}

/*
 * SetInitialProfiles: 设置初始条件 in cc(浓度场), cp(导数), and id(微分/代数
 * 类型). 使用多项式类型作为 prey cc 的初始值, 使用 常数 (1.0e5) 作为 predator
 * cc 的初始值 id 对于 prey 设置为1 (微分分量), 对于predators设置为 0 (代数分量)
 * 根据给定 system 设置 prey 的 cp 值; 将predator的cp 值设定为零.
 */
static void SetInitialProfiles(N_Vector cc, N_Vector cp, N_Vector id, UserData webdata)
{
    sunindextype loc, yloc, is, jx, jy, np;
    realtype xx, yy, xyfactor;
    realtype *ccv, *cpv, *idv;

    ccv = NV_DATA_OMP(cc);
    cpv = NV_DATA_OMP(cp);
    idv = NV_DATA_OMP(id);
    np = webdata->np;

    /* Loop over grid, load cc 值和 id 值. */
    for (jy = 0; jy < MY; jy++)
    {
        yy = jy * webdata->dy;
        yloc = NSMX * jy;  // jy 列
        for (jx = 0; jx < MX; jx++)
        {
            xx = jx * webdata->dx;
            xyfactor = RCONST(16.0) * xx * (ONE - xx) * yy * (ONE - yy);
            xyfactor *= xyfactor;
            loc = yloc + NUM_SPECIES * jx;  // 行列(jx, jy)对应的一维 位置

            for (is = 0; is < NUM_SPECIES; is++)
            {
                if (is < np)
                {
                    ccv[loc + is] = RCONST(10.0) + (realtype)(is + 1) * xyfactor;
                    idv[loc + is] = ONE;
                }
                else
                {
                    ccv[loc + is] = RCONST(1.0e5);
                    idv[loc + is] = ZERO;
                }
            }
        }
    }

    /* 设置 prey的cp(浓度场导数), 通过调用函数 Fweb. */
    Fweb(ZERO, cc, cp, webdata);

    /* 将 predators 的 c'(导数) 设置为 0. */
    for (jy = 0; jy < MY; jy++)
    {
        yloc = NSMX * jy;
        for (jx = 0; jx < MX; jx++)
        {
            loc = yloc + NUM_SPECIES * jx;
            for (is = np; is < NUM_SPECIES; is++)
            {
                cpv[loc + is] = ZERO;
            }
        }
    }
}

/*
 * Print first lines of output (problem description)
 */
static void PrintHeader(sunindextype mu, sunindextype ml, realtype rtol, realtype atol)
{
    printf(
        "\nidaFoodWeb_bnd_omp: Predator-prey DAE OpenMP example problem for "
        "IDA \n\n");
    printf("Number of species ns: %d", NUM_SPECIES);
    printf("     Mesh dimensions: %d x %d", MX, MY);
    printf("     System size: %d\n", NEQ);
#if defined(SUNDIALS_EXTENDED_PRECISION)
    printf("Tolerance parameters:  rtol = %Lg   atol = %Lg\n", rtol, atol);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
    printf("Tolerance parameters:  rtol = %g   atol = %g\n", rtol, atol);
#else
    printf("Tolerance parameters:  rtol = %g   atol = %g\n", rtol, atol);
#endif
    printf("Linear solver: SUNBAND,  Band parameters mu = %ld, ml = %ld\n", (long int)mu, (long int)ml);
    printf("CalcIC called to correct initial predator concentrations.\n\n");
    printf("-----------------------------------------------------------\n");
    printf("  t        bottom-left  top-right");
    printf("    | nst  k      h\n");
    printf("-----------------------------------------------------------\n\n");
}

/*
 * PrintOutput: Print output values at output time t = tt.
 * Selected run statistics are printed.  Then values of the concentrations
 * are printed for the bottom left and top right grid points only.
 */

static void PrintOutput(void *ida_mem, N_Vector c, realtype t)
{
    int i, kused, retval;
    long int nst;
    realtype *c_bl, *c_tr, hused;

    retval = IDAGetLastOrder(ida_mem, &kused);
    check_retval(&retval, "IDAGetLastOrder", 1);
    retval = IDAGetNumSteps(ida_mem, &nst);
    check_retval(&retval, "IDAGetNumSteps", 1);
    retval = IDAGetLastStep(ida_mem, &hused);
    check_retval(&retval, "IDAGetLastStep", 1);

    c_bl = IJ_Vptr(c, 0, 0);
    c_tr = IJ_Vptr(c, MX - 1, MY - 1);

#if defined(SUNDIALS_EXTENDED_PRECISION)
    printf("%8.2Le %12.4Le %12.4Le   | %3ld  %1d %12.4Le\n", t, c_bl[0], c_tr[0], nst, kused, hused);
    for (i = 1; i < NUM_SPECIES; i++) printf("         %12.4Le %12.4Le   |\n", c_bl[i], c_tr[i]);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
    printf("%8.2e %12.4e %12.4e   | %3ld  %1d %12.4e\n", t, c_bl[0], c_tr[0], nst, kused, hused);
    for (i = 1; i < NUM_SPECIES; i++) printf("         %12.4e %12.4e   |\n", c_bl[i], c_tr[i]);
#else
    printf("%8.2e %12.4e %12.4e   | %3ld  %1d %12.4e\n", t, c_bl[0], c_tr[0], nst, kused, hused);
    for (i = 1; i < NUM_SPECIES; i++) printf("         %12.4e %12.4e   |\n", c_bl[i], c_tr[i]);
#endif

    printf("\n");
}

/*
 * PrintFinalStats: Print final run data contained in iopt.
 */

static void PrintFinalStats(void *ida_mem)
{
    long int nst, nre, nreLS, nni, nje, netf, ncfn;
    int retval;

    retval = IDAGetNumSteps(ida_mem, &nst);
    check_retval(&retval, "IDAGetNumSteps", 1);
    retval = IDAGetNumNonlinSolvIters(ida_mem, &nni);
    check_retval(&retval, "IDAGetNumNonlinSolvIters", 1);
    retval = IDAGetNumResEvals(ida_mem, &nre);
    check_retval(&retval, "IDAGetNumResEvals", 1);
    retval = IDAGetNumErrTestFails(ida_mem, &netf);
    check_retval(&retval, "IDAGetNumErrTestFails", 1);
    retval = IDAGetNumNonlinSolvConvFails(ida_mem, &ncfn);
    check_retval(&retval, "IDAGetNumNonlinSolvConvFails", 1);
    retval = IDAGetNumJacEvals(ida_mem, &nje);
    check_retval(&retval, "IDAGetNumJacEvals", 1);
    retval = IDAGetNumLinResEvals(ida_mem, &nreLS);
    check_retval(&retval, "IDAGetNumLinResEvals", 1);

    printf("-----------------------------------------------------------\n");
    printf("Final run statistics: \n\n");
    printf("Number of steps                    = %ld\n", nst);
    printf("Number of residual evaluations     = %ld\n", nre + nreLS);
    printf("Number of Jacobian evaluations     = %ld\n", nje);
    printf("Number of nonlinear iterations     = %ld\n", nni);
    printf("Number of error test failures      = %ld\n", netf);
    printf("Number of nonlinear conv. failures = %ld\n", ncfn);
}

/*
 * Fweb: Rate function for the food-web problem.
 * This routine computes the right-hand sides of the system equations,
 * consisting of the diffusion term and interaction term.
 * The interaction term is computed by the function WebRates.
 */
static void Fweb(realtype tcalc, N_Vector cc, N_Vector crate, UserData webdata)
{
    sunindextype jx, jy, is, idyu, idyl, idxu, idxl;
    realtype xx, yy, *cxy, *ratesxy, *cratexy, dcyli, dcyui, dcxli, dcxui;

    /* Loop over grid points, evaluate interaction vector (length ns),
     form diffusion difference terms, and load crate. */
    jx = jy = is = 0;
    for (jy = 0; jy < MY; jy++)
    {                                          // 在 mesh y 节点上循环
        yy = (webdata->dy) * jy;               // y坐标
        idyu = (jy != MY - 1) ? NSMX : -NSMX;  // y 方向的 上节点
        idyl = (jy != 0) ? NSMX : -NSMX;       // y 方向的 下节点
        for (jx = 0; jx < MX; jx++)
        {                                                        // 在 mesh x 节点上循环
            xx = (webdata->dx) * jx;                             // x坐标
            idxu = (jx != MX - 1) ? NUM_SPECIES : -NUM_SPECIES;  // the node y direction upper neighbor
            idxl = (jx != 0) ? NUM_SPECIES : -NUM_SPECIES;       // x lower
            cxy = IJ_Vptr(cc, jx, jy);                           // the node pointer
            ratesxy = IJ_Vptr(webdata->rates, jx, jy);
            cratexy = IJ_Vptr(crate, jx, jy);

            /* Get interaction vector at this grid point. */
            WebRates(xx, yy, cxy, ratesxy, webdata);

            /* Loop over species, do differencing, load crate segment. */
#pragma omp parallel for default(shared) private(is, dcyli, dcyui, dcxli, dcxui) schedule(static) num_threads(webdata->nthreads)
            // loop on species
            for (is = 0; is < NUM_SPECIES; is++)
            {
                /* y 的二阶差分公式*/
                dcyli = *(cxy + is) - *(cxy - idyl + is);
                dcyui = *(cxy + idyu + is) - *(cxy + is);

                /* x 的二阶差分 */
                dcxli = *(cxy + is) - *(cxy - idxl + is);
                dcxui = *(cxy + idxu + is) - *(cxy + is);

                /* Compute the crate values at (xx,yy). */
                cratexy[is] = coy[is] * (dcyui - dcyli) + cox[is] * (dcxui - dcxli) + ratesxy[is];

            } /* End is loop */
        }     /* End of jx loop */
    }         /* End of jy loop */
}

/*
 * WebRates: Evaluate reaction rates at a given spatial point.
 * At a given (x,y), evaluate the array of ns reaction terms R.
 */
static void WebRates(realtype xx, realtype yy, realtype *cxy, realtype *ratesxy, UserData webdata)
{
    int is;
    realtype fac;

    for (is = 0; is < NUM_SPECIES; is++) ratesxy[is] = dotprod(NUM_SPECIES, cxy, acoef[is]);
    // coefficient bi
    fac = ONE + ALPHA * xx * yy + BETA * sin(FOURPI * xx) * sin(FOURPI * yy);

    for (is = 0; is < NUM_SPECIES; is++) ratesxy[is] = cxy[is] * (bcoef[is] * fac + ratesxy[is]);
}

/*
 * dotprod: dot product routine for realtype arrays, for use by WebRates.
 */

static realtype dotprod(sunindextype size, realtype *x1, realtype *x2)
{
    sunindextype i;
    realtype *xx1, *xx2, temp = ZERO;

    xx1 = x1;
    xx2 = x2;
    for (i = 0; i < size; i++) temp += (*xx1++) * (*xx2++);
    return (temp);
}

/*
 * 检查函数返回值, opt 控制不同的检查行为
 *   opt == 0 对应 SUNDIALS 函数分配内存, 检查是否返回 NULL 指针;
 *   opt == 1 对应 SUNDIALS function 返回整数值, 检查是否 retval < 0;
 *   opt == 2 对应 function 分配内存, 检查是否返回 NULL 指针;
 */

static int check_retval(void *returnvalue, char *funcname, int opt)
{
    int *retval;

    if (opt == 0 && returnvalue == NULL)
    {
        /* 检查 SUNDIALS 函数是否返回 NULL pointer - no memory allocated */
        fprintf(stderr, "\nSUNDIALS_ERROR: %s() failed - returned NULL pointer\n\n", funcname);
        return (1);
    }
    else if (opt == 1)
    {
        /* Check if retval < 0 */
        retval = (int *)returnvalue;
        if (*retval < 0)
        {
            fprintf(stderr, "\nSUNDIALS_ERROR: %s() failed with retval = %d\n\n", funcname, *retval);
            return (1);
        }
    }
    else if (opt == 2 && returnvalue == NULL)
    {
        /* Check if function returned NULL pointer - no memory allocated */
        fprintf(stderr, "\nMEMORY_ERROR: %s() failed - returned NULL pointer\n\n", funcname);
        return (1);
    }

    return (0);
}
